/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";

let projId = getArgs().id;
let splrId = sessionStorage.getItem('splrId');
let fullNam = sessionStorage.getItem('fullNam');
let token = sessionStorage.getItem('supplier_token');
if(fullNam !=null && fullNam !='' && fullNam !='null'){
    $(".J_splrNam").val(fullNam);
}
if(!token){
    alert('请先登录');
    window.location=HOST_FRT+'/portal/login.html';
}
if (!splrId){
    alert('请先完善注册信息');
    window.location=HOST_FRT+'/supplier_self/user_register.html';
}
if(!projId){
    alert("参数无效");
    window.history.back();
}


$('.J_add-user-form').validate({
    errorElement: 'label', //default input error message container
    errorClass: 'help-inline', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        splrCtct: {
            required: true
        },
        splrCtctTel: {
            required: true
        },
        splrNam:{
            required: true
        }
    },
    message:{

    },

    invalidHandler: function (event, validator) { //display error alert on form submit
        $('.alert-error', $('.J_add-user-form')).show();
    },

    highlight: function (element) { // hightlight error inputs
        $(element).closest('.control-group').addClass('error'); // set error class to the control group
    },

    success: function (label) {
        label.closest('.control-group').removeClass('error');
        label.remove();
    },

    errorPlacement: function (error, element) {
        error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
    },

    submitHandler: function (form) {
        let $form = $(form);
        let splrCtct = $form.find('.J_splrCtct')[0].value;
        let splrCtctTel = $form.find('.J_splrCtctTel')[0].value;
        let splrNam = $form.find('.J_splrNam')[0].value;
        let memo = $form.find('.J_memo')[0].value;
        // let fileUpload = '';
        let data = {
            "projId":projId,
            "splrId":splrId,
            "splrCtct":splrCtct,
            "splrCtctTel":splrCtctTel,
            "splrNam":splrNam,
            "dpstSts":'0',
            "dpstPic":'',
            "memo":memo
        }

        $.ajax({
            url: HOST_ADDR + '/cpmp/xjProjMngForSplr/bidApp',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp){
                console.log(resp);
                if(resp.success){
                    alert('申请成功');
                }
                else{
                    alert(resp.msgInf);
                }
                window.history.back();
            },
            error:function(err){
                console.log(err);
            }
        });
    }
});

$('.J_add-user-form .J_cancel').click(function(){
    location.href = 'enquiry_list.html';
});
